|
1
|
|
|
import CommentsApi from '../api/restful-api'; |
|
2
|
|
|
|
|
3
|
2 |
|
let api = new CommentsApi(); |
|
4
|
|
|
|
|
5
|
2 |
|
export const loadComments = function(context, data) { |
|
6
|
|
|
return api.getComments( |
|
7
|
|
|
data.model, |
|
8
|
|
|
data.modelId, |
|
9
|
|
|
data.parentId, |
|
10
|
|
|
typeof data.params === 'object' ? data.params : {} |
|
11
|
|
|
).then(function(result) { |
|
12
|
|
|
result.data.comments.forEach((comment) => { |
|
13
|
|
|
context.commit('addComment', comment) |
|
14
|
|
|
// Handle child comments if there are any |
|
15
|
|
|
comment.comments.forEach((comment) => { |
|
16
|
|
|
context.commit('addComment', comment); |
|
17
|
|
|
|
|
18
|
8 |
|
if (comment.comments !== undefined && comment.comments.length > 0) { |
|
19
|
|
|
for (var i in comment.comments) { |
|
|
|
|
|
|
20
|
|
|
comment.comments[i].created = new Date(comment.comments[i].created); |
|
21
|
|
|
context.commit('addComment', comment); |
|
22
|
|
|
} |
|
23
|
|
|
} |
|
24
|
|
|
}); |
|
25
|
|
|
|
|
26
|
|
|
context.commit('updatePagination', { |
|
27
|
|
|
model: data.model, |
|
28
|
|
|
modelId: data.modelId, |
|
29
|
|
|
parentId: data.parentId, |
|
30
|
|
|
pagination: result.data.pagination |
|
31
|
|
|
}); |
|
32
|
|
|
}); |
|
33
|
|
|
}); |
|
34
|
|
|
}; |
|
35
|
|
|
|
|
36
|
2 |
|
export const deleteComment = function(context, data) { |
|
37
|
|
|
return api.remove( |
|
38
|
|
|
data.model, |
|
39
|
|
|
data.foreign_key, |
|
40
|
|
|
data.id |
|
41
|
|
|
).then(function(result) { |
|
42
|
|
|
context.commit('deleteComment', result.data.comment); |
|
43
|
|
|
}); |
|
44
|
|
|
}; |
|
45
|
|
|
|
|
46
|
2 |
|
export const updateComment = function(context, data) { |
|
47
|
|
|
return api.update( |
|
48
|
|
|
data.model, |
|
49
|
|
|
data.foreign_key, |
|
50
|
|
|
data.id, |
|
51
|
|
|
data |
|
52
|
|
|
).then(function(result) { |
|
53
|
|
|
context.commit('addComment', result.data.comment); |
|
54
|
|
|
}); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
2 |
|
export const addComment = function(context, data) { |
|
58
|
|
|
return api.add( |
|
59
|
|
|
data.model, |
|
60
|
|
|
data.foreign_key, |
|
61
|
|
|
data |
|
62
|
|
|
).then(function(result) { |
|
63
|
|
|
context.commit('addComment', result.data.comment); |
|
64
|
|
|
context.commit('setLastCommentTime'); |
|
65
|
|
|
}); |
|
66
|
|
|
} |
|
67
|
|
|
|
When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically: